#include "luntan.h"
#include "ui_luntan.h"

#include "flowcontroler.h"
#include "postcard.h"
#include "writepostdialog.h"

#include <QScrollBar>
#include <QDebug>
class PostItem : public QListWidgetItem
{
public:
    PostItem(Core::Post &post)
    {
        m_card = new PostCard();
        m_card->setPost(post);
        setSizeHint(m_card->sizeHint());
//        if(post.comments.isEmpty())
//        {
//            setSizeHint(QSize(500, 230-76));
//        }
//        else
//        {
//            setSizeHint(QSize(500, 230));
//        }

    }

    Core::Post getPost()
    {
        return m_card->getPost();
    }

    void setPixmapBuf(QByteArray buf)
    {
        m_card->setPixmapBuf(buf);
    }

    PostCard *card()
    {
        return m_card;
    }

private:
    PostCard *m_card;
};


Luntan::Luntan(QWidget *parent) : QWidget(parent),
    ui(new Ui::Luntan)
{
    ui->setupUi(this);

//    QLabel *label = new QLabel(this);
//    label->setText("luntan");
//    label->move(50, 50); // 设置标签的位置


    connect(FlowControler::Interface, &XInterface::getAllPostsFinished, this, [ = ](int ret, QList<Core::Post> posts)
    {
        if(ret == NoError)
        {
            updatePostList(posts);
        }

    });

    connect(FlowControler::Interface, &XInterface::sig_havePixPostShow, this, [ = ](int ret, Core::PixFromPost info)
    {
        if(ret == NoError)
        {
            for (int i = 0; i < ui->listWidget->count(); ++i)
            {
                PostItem *item = dynamic_cast<PostItem*>(ui->listWidget->item(i));
                qDebug() << item->getPost().id << "cur id";
                if(item->getPost().id == info.postId)
                {
                    item->setPixmapBuf(info.cell_pix_buf);
                }
            }
        }
        else
        {
            qDebug() << "Error";
        }

    });
//    FlowControler::Interface->getPosts(10);

    ui->listWidget->setStyleSheet("QListWidget::item { background-color: transparent; }");
    ui->listWidget->setSpacing(2);
    // 设置滚动模式为按像素滚动
    ui->listWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);

    // 可选：自定义滚动速度
    QScrollBar* vScrollBar = ui->listWidget->verticalScrollBar();
    vScrollBar->setSingleStep(10); // 每次滚动 10 像素
}

void Luntan::updatePostList(QList<Core::Post> &posts)
{
    ui->listWidget->clear();
    for(auto &p : posts)
    {
        PostItem *item = new PostItem(p);
        ui->listWidget->addItem(item);
        ui->listWidget->setItemWidget(item, item->card());
    }
}

void Luntan::on_btnPost_clicked()
{
    WritePostDialog dialog;
    dialog.exec();
}

void Luntan::on_btnUpdate_clicked()
{
    FlowControler::Interface->getPosts(10);
}

void Luntan::on_btnQuery_clicked()
{
    QString searchText = ui->edit_search->text();
    for (int i = 0; i < ui->listWidget->count(); ++i)
    {
        PostItem *item = dynamic_cast<PostItem*>(ui->listWidget->item(i));
        if (searchText.isEmpty())
        {
            item->setHidden(false);
        }
        else
        {
            item->setHidden(!item->getPost().title.contains(searchText, Qt::CaseInsensitive));
        }
    }
}
